﻿String.prototype.trim = _String_trim;
String.prototype.replaceAll = _String_replaceAll;
var DetailArray=new Array();

function _String_trim() {
	if (typeof(this)!="undefined") {
		return (""+this).replace(/^\s+/g, '').replace(/\s+$/g, '');
	}
	else {
		return this;
	}
} 
function _String_replaceAll(StrOld, StrNew) {
	if (typeof(this)!="undefined") {
		return this.replace(new RegExp(StrOld,"g"),StrNew);
	}
	else {
		return this;
	}
}

/*
	================================================================================================== 
			             中菲電腦股份有限公司   專業軟體事業部                                     


      程式名稱   :          CompareValue(val1,val2,type)                                            

      程式設計師 :           Jacky                                               

      編寫日期   :           2005/03/15 
	      
	function功能: 判斷起迄值

	傳入值 : val1(起值ID),val2(迄值ID),type(型態：S.字串、N.數字)
	傳出值 : result(0.判斷成功、1.起迄值為空、2.起值不可以大於迄值、3.起迄值型態錯誤、4.轉換型態錯誤)
*/
function CompareValue(val1,val2,type)
{
	var result = new Object();
	if($(val1).val() == "" && $(val2).val() =="")
	{
			result.id = "1";
			result.value = "起迄值為空";
	}
	else
	{
	    if($(val1).val() == "")
		{
		    if(type == "N")
		    {
		        if (isNaN($(val1).val()) || isNaN($(val2).val()))
		        {
		            result.id = "3";
		            result.value = "起迄值型態錯誤";
		        }
		        else
		        {
		            $(val1).val($(val2).val());
		            result.id = "0";
		            result.value = "判斷成功";
		        }
		        return result;
		    }
		    else if($(val2).val() == "")
		    {
		        if(type == "N")
		        {
		            if (isNaN($(val1).val()) || isNaN($(val2).val()))
		            {
		                result.id = "3";
		                result.value = "起迄值型態錯誤";
		            }
		            else
		            {
		                $(val2).val($(val1).val());
		                result.id = "0";
		                result.value = "判斷成功";
		            }
		        }
		        else
		        {
		            $(val2).val($(val1).val());
		            result.id = "0";
		            result.value = "判斷成功";
		        }
		        return result;
		    }
		
		
		if(type == "S")
		{
		    if($(val1).val() > $(val2).val())
			{
				result.id = "2";
				result.value = "起值不可以大於迄值";
			}
			else
			{
				result.id = "0";
				result.value = "判斷成功";
			}
		}
		else if(type == "N")
		{
		    if(isNaN($(val1).val()) || isNaN($(val2).val()))
			{
				result.id = "3";
				result.value = "起迄值型態錯誤";
			}
			else
			{
		        if(parseInt($(val1).val(),10) > parseInt($(val2).val(),10))
				{
					result.id = "2";
					result.value = "起值不可以大於迄值";
				}
				else
				{
					result.id = "0";
					result.value = "判斷成功";
				}
			}
		}
		else
		{
			result.id = "4";
			result.value = "轉換型態錯誤";
		}
	}
return result;
}

/*
	================================================================================================== 
			             中菲電腦股份有限公司   專業軟體事業部                                     


      程式名稱   :          Trim(string)                                          

      程式設計師 :           Vicky                                               

      編寫日期   :           2005/03/17 
	      
	function功能: 將空白字串刪除

	傳入值 : string
	傳出值 : string
*/
function Trim(LSTR_String)
{
	var LINT_I;
	var LINT_Length;
	if(LSTR_String==null)
	{
		return LSTR_String;
	}
	else
	{
		LINT_Length=LSTR_String.length;
	}
	for(LINT_I=1;LINT_I<=LINT_Length;LINT_I++)
	{
		if(LSTR_String.indexOf(" ")==0)
		{
			LSTR_String=LSTR_String.substr(1);
		}
		else
		{
			break;
		}
	} 
	LINT_Length=LSTR_String.length;
	for(LINT_I=1;LINT_I<=LINT_Length;LINT_I++)
	{
		if(LSTR_String.lastIndexOf(" ")==LSTR_String.length-1)
		{
			LSTR_String=LSTR_String.substr(0,LSTR_String.length-1);
		}
		else
		{
			break;
		}
	}  
	return LSTR_String;
}


/*
	================================================================================================== 
			             中菲電腦股份有限公司   專業軟體事業部 

      程式名稱   :          CheckGridData(check_col,"DetailTable","DetailTitle",chkSTATUS)                                          

      程式設計師 :           Jacky                                               

      編寫日期   :           2005/03/16 
	  
	  修改日期   :           2005/03/29(增加判斷刪除CHECK BOX一旦勾選不去檢核功能)
							 2005/08/15(修改整個function)
	function功能: 畫面資料重複檢核
	傳入值 : check_col(被檢查欄位的index),DetailTable(被檢查欄位的Table名稱),DetailTitle(被檢查欄位Title的Table名稱),chkSTATUS(刪除CHECK BOX的index)
	傳出值 : 無

*/
function CheckGridData(check_col,DetailTable,DetailTitle,chkSTATUS)
{		
	try 
	{	
		var objControl = new Object();
		var check_col = check_col.split(",");
		var title;
		var result = true;
		//清除錯誤訊息
		for(var k=0;k<$(DetailTable)[0].rows.length;k++)
 		{
 			objControl.ErrCode="111"
 			objControl.id = $(DetailTable)[0].rows(k).cells(parseInt(check_col[0])).firstChild.name;
 			ErrHandler_RemoveItem(objControl)
		}
		
		//產生所需要的陣列
		for(var j=0;j<36;j++)
		{
			DetailArray[j]=new Array();
		}
		
		title = $(DetailTable)[0].rows(0).cells(parseInt(check_col[0])).innerText;
		
		//巡覽畫面上所有的資料
		for(var i=0;i<$(DetailTable)[0].rows.length;i++)
		{					
			var chkVALUE = '';
			for(var w=0;w<check_col.length;w++)
			{
			    chkVALUE = chkVALUE + $(DetailTable)[0].rows(i).cells(parseInt(check_col[w])).firstChild.value.trim().toUpperCase();
			}
			objControl.id = $(DetailTable)[0].rows(i).cells(parseInt(check_col[0])).firstChild.name;
			objControl.ErrCode="111";
			//判斷畫面是否需要檢查刪除CHECK BOX
			if(chkSTATUS!='')
			{
				//若判斷刪除CHECK BOX是否有勾選

			    if($(DetailTable)[0].rows(i).cells(parseInt(chkSTATUS)).firstChild.checked==false)
				{
					switch(chkVALUE.slice(-1))
					{
						case "0": 				
							//檢查是否有重複資料

							result = CheckData(DetailArray[0],chkVALUE,objControl,title,i)	
							break;		
						case "1":
							result = CheckData(DetailArray[1],chkVALUE,objControl,title,i)	
							break;	
						case "2":
							result = CheckData(DetailArray[2],chkVALUE,objControl,title,i)	
							break;	
						case "3":
							result = CheckData(DetailArray[3],chkVALUE,objControl,title,i)	
							break;	
						case "4":
							result = CheckData(DetailArray[4],chkVALUE,objControl,title,i)	
							break;	
						case "5":
							result = CheckData(DetailArray[5],chkVALUE,objControl,title,i)	
							break;	
						case "6":
							result = CheckData(DetailArray[6],chkVALUE,objControl,title,i)	
							break;	
						case "7":
							result = CheckData(DetailArray[7],chkVALUE,objControl,title,i)	
							break;	
						case "8":
							result = CheckData(DetailArray[8],chkVALUE,objControl,title,i)	
							break;	
						case "9":
							result = CheckData(DetailArray[9],chkVALUE,objControl,title,i)	
							break;	
						case "A": 
							result = CheckData(DetailArray[10],chkVALUE,objControl,title,i)	
							break;		
						case "B":
							result = CheckData(DetailArray[11],chkVALUE,objControl,title,i)	
							break;	
						case "C":
							result = CheckData(DetailArray[12],chkVALUE,objControl,title,i)	
							break;	
						case "D":
							result = CheckData(DetailArray[13],chkVALUE,objControl,title,i)	
							break;	
						case "E":
							result = CheckData(DetailArray[14],chkVALUE,objControl,title,i)	
							break;	
						case "F":
							result = CheckData(DetailArray[15],chkVALUE,objControl,title,i)	
							break;	
						case "G":
							result = CheckData(DetailArray[16],chkVALUE,objControl,title,i)	
							break;	
						case "H":
							result = CheckData(DetailArray[17],chkVALUE,objControl,title,i)	
							break;	
						case "I":
							result = CheckData(DetailArray[18],chkVALUE,objControl,title,i)	
							break;	
						case "J":
							result = CheckData(DetailArray[19],chkVALUE,objControl,title,i)	
							break;	
						case "K": 
							result = CheckData(DetailArray[20],chkVALUE,objControl,title,i)	
							break;		
						case "L":
							result = CheckData(DetailArray[21],chkVALUE,objControl,title,i)	
							break;	
						case "M":
							result = CheckData(DetailArray[22],chkVALUE,objControl,title,i)	
							break;	
						case "N":
							result = CheckData(DetailArray[23],chkVALUE,objControl,title,i)	
							break;	
						case "O":
							result = CheckData(DetailArray[24],chkVALUE,objControl,title,i)	
							break;	
						case "P":
							result = CheckData(DetailArray[25],chkVALUE,objControl,title,i)	
							break;	
						case "Q":
							result = CheckData(DetailArray[26],chkVALUE,objControl,title,i)	
							break;	
						case "R":
							result = CheckData(DetailArray[27],chkVALUE,objControl,title,i)	
							break;	
						case "S":
							result = CheckData(DetailArray[28],chkVALUE,objControl,title,i)	
							break;	
						case "T":
							result = CheckData(DetailArray[29],chkVALUE,objControl,title,i)	
							break;
						case "U": 
							result = CheckData(DetailArray[30],chkVALUE,objControl,title,i)	
							break;		
						case "V":
							result = CheckData(DetailArray[31],chkVALUE,objControl,title,i)	
							break;	
						case "W":
							result = CheckData(DetailArray[32],chkVALUE,objControl,title,i)	
							break;	
						case "X":
							result = CheckData(DetailArray[33],chkVALUE,objControl,title,i)	
							break;	
						case "Y":
							result = CheckData(DetailArray[34],chkVALUE,objControl,title,i)	
							break;	
						case "Z":
							result = CheckData(DetailArray[35],chkVALUE,objControl,title,i)	
							break;
					}
				}
			}
			else
			{
				switch(chkVALUE.slice(-1))
				{
					case "0": 				
						//檢查是否有重複資料

						result = CheckData(DetailArray[0],chkVALUE,objControl,title,i)	
						break;		
					case "1":
						result = CheckData(DetailArray[1],chkVALUE,objControl,title,i)	
						break;	
					case "2":
						result = CheckData(DetailArray[2],chkVALUE,objControl,title,i)	
						break;	
					case "3":
						result = CheckData(DetailArray[3],chkVALUE,objControl,title,i)	
						break;	
					case "4":
						result = CheckData(DetailArray[4],chkVALUE,objControl,title,i)	
						break;	
					case "5":
						result = CheckData(DetailArray[5],chkVALUE,objControl,title,i)	
						break;	
					case "6":
						result = CheckData(DetailArray[6],chkVALUE,objControl,title,i)	
						break;	
					case "7":
						result = CheckData(DetailArray[7],chkVALUE,objControl,title,i)	
						break;	
					case "8":
						result = CheckData(DetailArray[8],chkVALUE,objControl,title,i)	
						break;	
					case "9":
						result = CheckData(DetailArray[9],chkVALUE,objControl,title,i)	
						break;	
					case "A": 
						result = CheckData(DetailArray[10],chkVALUE,objControl,title,i)	
						break;		
					case "B":
						result = CheckData(DetailArray[11],chkVALUE,objControl,title,i)	
						break;	
					case "C":
						result = CheckData(DetailArray[12],chkVALUE,objControl,title,i)	
						break;	
					case "D":
						result = CheckData(DetailArray[13],chkVALUE,objControl,title,i)	
						break;	
					case "E":
						result = CheckData(DetailArray[14],chkVALUE,objControl,title,i)	
						break;	
					case "F":
						result = CheckData(DetailArray[15],chkVALUE,objControl,title,i)	
						break;	
					case "G":
						result = CheckData(DetailArray[16],chkVALUE,objControl,title,i)	
						break;	
					case "H":
						result = CheckData(DetailArray[17],chkVALUE,objControl,title,i)	
						break;	
					case "I":
						result = CheckData(DetailArray[18],chkVALUE,objControl,title,i)	
						break;	
					case "J":
						result = CheckData(DetailArray[19],chkVALUE,objControl,title,i)	
						break;	
					case "K": 
						result = CheckData(DetailArray[20],chkVALUE,objControl,title,i)	
						break;		
					case "L":
						result = CheckData(DetailArray[21],chkVALUE,objControl,title,i)	
						break;	
					case "M":
						result = CheckData(DetailArray[22],chkVALUE,objControl,title,i)	
						break;	
					case "N":
						result = CheckData(DetailArray[23],chkVALUE,objControl,title,i)	
						break;	
					case "O":
						result = CheckData(DetailArray[24],chkVALUE,objControl,title,i)	
						break;	
					case "P":
						result = CheckData(DetailArray[25],chkVALUE,objControl,title,i)	
						break;	
					case "Q":
						result = CheckData(DetailArray[26],chkVALUE,objControl,title,i)	
						break;	
					case "R":
						result = CheckData(DetailArray[27],chkVALUE,objControl,title,i)	
						break;	
					case "S":
						result = CheckData(DetailArray[28],chkVALUE,objControl,title,i)	
						break;	
					case "T":
						result = CheckData(DetailArray[29],chkVALUE,objControl,title,i)	
						break;
					case "U": 
						result = CheckData(DetailArray[30],chkVALUE,objControl,title,i)	
						break;		
					case "V":
						result = CheckData(DetailArray[31],chkVALUE,objControl,title,i)	
						break;	
					case "W":
						result = CheckData(DetailArray[32],chkVALUE,objControl,title,i)	
						break;	
					case "X":
						result = CheckData(DetailArray[33],chkVALUE,objControl,title,i)	
						break;	
					case "Y":
						result = CheckData(DetailArray[34],chkVALUE,objControl,title,i)	
						break;	
					case "Z":
						result = CheckData(DetailArray[35],chkVALUE,objControl,title,i)	
						break;
				}
			}
		}
	}
	catch (e)
	{
        ShowError(e.Message);
	}
	return result;						
}


/*
	================================================================================================== 
			             中菲電腦股份有限公司   專業軟體事業部   

      程式名稱   :			CheckData(chkArray,chkVALUE,objControl,title,idx)                                           

      程式設計師 :			Jacky                                               

      編寫日期   :			2005/08/15 
	      
	function功能: 檢查所需檢查資料是否在陣列內
	傳入值 :chkArray(所要檢查之陣列，chkVALUE(所要檢查之資料)，objControl(產生錯誤訊息所需之物件)，

			title(產生錯誤訊息所需之欄位名),idx(所要檢查資料的SEQ)
	傳出值 : 無

*/
function CheckData(chkArray,chkVALUE,objControl,title,idx)
{	
	try 
	{		
		var chk = true;
		//判斷是否是此陣列第一筆資料

		if(chkArray.length != 0)
		{
			//去所屬陣列檢查是否有重複資料
			for(var w=0;w<chkArray.length;w++)
			{
				if(chkArray[w] == chkVALUE)
				{
					chk = false;
					addDetailErrorItem(objControl,":第"+(idx+1)+"筆資料重複!",title)	
				}
			}
			//若無重複資料則將此筆資料加入陣列
			if(chk)	
			{	
				chkArray.push(chkVALUE);
			}
		}
		else	
		{	
			chkArray.push(chkVALUE);
		}
	}
	catch (e)
	{
        ShowError(e.Message);
	}
	return chk;	
}
		